<?php

class EditselfadminController extends Controller
{
	/**
	* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	* using two-column layout. See 'protected/views/layouts/column2.php'.
	*/
	public $layout='//layouts/column1';
	
	public function init() {
		$this->setTitleBarTitle( '管理员帐号管理');
	}

	public function getPageOptionsHtml()
	{
		return sprintf("%s\n%s", BSHtml::buttonDropdown('常用操作', $this->menu), parent::getPageOptionsHtml());
	}
	
	/**
	* @return array action filters
	*/
	public function filters()
	{
		return [
			'accessControl', // perform access control for CRUD operations
// 			'postOnly + delete', // we only allow deletion via POST request
		];
	}
	
	/**
	* Specifies the access control rules.
	* This method is used by the 'accessControl' filter.
	* @return array access control rules
	*/
	public function accessRules()
	{
		return [
			['allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>['index','view','editDatum','EditPwd'],
				'users'=>['*'],
			],
		
			['deny',  // deny all users
				'users'=>['*'],
			],
		];
	}
	
	
	
	/**
	* Lists all models.
	*/
	public function actionIndex()
	{
		$file = dirname(dirname(__FILE__)).'/views/editselfadmin/editpwd.php';
		echo time(),"||||";
		if(is_file($file))
		{
			echo "存在文件：".time();
		}
		else 
		{
			echo "文件不错在:";
		}
		echo $file;
		exit;
		//$this->forward('admin');
	}
	

	
	
	/*
	 * 管理员修改自身的资料信息
	 */
	public function actionEditdatum()
	{
		$id = intval(Yii::app()->user->user_id);
		if($id == 0)
		{
			//您无权编辑
			$this->redirect(Yii::app()->getBaseUrl().Yii::app()->homeUrl);
		}
		$model = new EditDatumForm();
		$adminmodel = UserAdmin::model()->findByPk($id);
		
		if(isset($_POST['EditDatumForm']))
		{
			$model->attributes = $_POST['EditDatumForm'];
			if($model->validate())
			{
				$adminmodel->attributes = $model->attributes;
				if($adminmodel->save())
				{
					$this->redirect(Yii::app()->getBaseUrl().Yii::app()->homeUrl);
				}
			}
		}
		else 
		{
			$model->attributes = $adminmodel->attributes;
		}
		$this->render('editdatum',[
				'model'=>$model,
				]);
	}
	
	/*
	 * 管理员修改自身的密码
	 */
	public function actionEditpwd()
	{
		$id = intval(Yii::app()->user->user_id);

		if($id == 0)
		{
			//您无权编辑
			$this->redirect(Yii::app()->getBaseUrl().Yii::app()->homeUrl);
		}
		
		//$model = UserAdmin::model()->findAllByPk($id);
		
		$model = new EditPwdForm();
		
		if(isset($_POST['EditPwdForm']))
		{
			$model->attributes = $_POST['EditPwdForm'];
			$oldpwd =  $_POST['EditPwdForm']['oldpwd'];
			$adminmodel = UserAdmin::model()->findByPk($id);
			if($model->validate())
			{
				//旧密码是否相等
				if($adminmodel->hashPassword($oldpwd) == $adminmodel->password)
				{
					$adminmodel->attributes = $model->attributes;
					if($adminmodel->save())
					{
						$this->redirect(Yii::app()->getBaseUrl().Yii::app()->homeUrl);
					}
					
				}
				else 
				{
					$model->addError("oldpwd",'旧密码不正确');
					
				}

				
			}
			
		}
		
		$this->render('editpwd',[
				'model'=>$model,
				]);
		
	}
}